#include "loginform.h"
#include "ui_loginform.h"
#include "flatui.h"
#include "tool.h"
#include "db.h"
#include <QMessageBox>
#include <QCryptographicHash>

LoginForm::LoginForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginForm)
{
    ui->setupUi(this);
    this->a = new AdminForm();
    this->initForm();
}

LoginForm::~LoginForm()
{
    delete ui;
}

void LoginForm::initForm()
{
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);
    ui->widget_picture->setStyleSheet(QString("background-image:url(:/image/%1.png);").arg(4));
    FlatUI::setLineEditQss(ui->lineEdit_PIN, 5, 2, "#DCE4EC", "#1ABC9C");
    FlatUI::setLineEditQss(ui->lineEdit_user, 5, 2, "#DCE4EC", "#1ABC9C");
    FlatUI::setLineEditQss(ui->lineEdit_password, 5, 2, "#DCE4EC", "#1ABC9C");
    FlatUI::setPushButtonQss(ui->pushButton_Entry, 5, 8, "#3498DB", "#FFFFFF", "#5DACE4", "#E5FEFF", "#2483C7", "#A0DAFB");
    FlatUI::setPushButtonQss(ui->pushButton_Reflash, 5, 8, "#3498DB", "#FFFFFF", "#5DACE4", "#E5FEFF", "#2483C7", "#A0DAFB");
    this->reflashPIN(); //初始化验证码
}

void LoginForm::clearLineEdit()
{
    ui->lineEdit_password->setText("");
    ui->lineEdit_PIN->setText("");
}

void LoginForm::closeAdminForm()
{
    if(!this->a->isHidden()) this->a->close();
}

void LoginForm::reflashPIN()
{
    ui->label_PIN->setText(Tool::RandomQString());
}
void LoginForm::on_pushButton_Close_clicked()
{
    this->close();
}

void LoginForm::on_pushButton_Reflash_clicked()
{
    this->reflashPIN();
}

void LoginForm::on_pushButton_Entry_clicked()
{
    //输入为空
    if (ui->lineEdit_user->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="not input the username";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_password->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="not input the password";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_PIN->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="not input the PIN";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    QSqlQuery dataSet = Db::getUserIfm(ui->lineEdit_user->text());
    //未找到用户
    if (!dataSet.next())
    {
        QString dlgTitle="fail";
        QString strInfo="no this user";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    //密码错误
    QString pwd = dataSet.value("salt").toString() + ui->lineEdit_password->text();
    QString md5pwd = QCryptographicHash::hash(pwd.toLatin1(),QCryptographicHash::Md5).toHex();
    if(dataSet.value("password").toString() != md5pwd)
    {
        QString dlgTitle="fail";
        QString strInfo="password error";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    //验证码错误
    if(ui->lineEdit_PIN->text() != ui->label_PIN->text())
    {
        this->reflashPIN();
        QString dlgTitle="fail";
        QString strInfo="PIN error";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (dataSet.value("ulevel").toInt() == 1)
    {
        if(a)
        {
            this->close();
            a->initIfm(dataSet.value("uid").toInt(),dataSet.value("username").toString(),dataSet.value("ulevel").toInt());
            a->show();
        }
        return;
    }
    if (dataSet.value("ulevel").toInt() == 2)
    {
        if(a)
        {
            this->close();
            a->initIfm(dataSet.value("uid").toInt(),dataSet.value("username").toString(),dataSet.value("ulevel").toInt());
            a->show();
        }
        return;
    }

}
